package Bill;

import java.util.*;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class Menu {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        BillManager billManager = new BillManager(0);

        boolean running = true;
        while (running) {
            System.out.println("=================================");
            System.out.println("欢迎使用个人账单管理系统");
            System.out.println("=================================");
            System.out.println("请选择操作：");
            System.out.println("1. 记录收入");
            System.out.println("2. 记录支出");
            System.out.println("3. 查看所有账单");
            System.out.println("4. 查询账单");
            System.out.println("5. 设置月度预算");
            System.out.println("6. 查看月度统计报告");
            System.out.println("7. 退出系统");
            System.out.print("请输入选项序号：");

            String choice = scanner.nextLine();

            switch (choice) {
                case "1":
                    System.out.println("请输入收入信息：");
                    System.out.print("日期（YYYY-MM-DD）：");
                    String dateStr = scanner.next();
                    System.out.print("金额：");
                    double amount = scanner.nextDouble();
                    System.out.print("类别（如工资、奖金等）：");
                    String category = scanner.next();
                    System.out.print("备注：");
                    String remark = scanner.next();
                    Date date = new Date(0);
                    try {
                        date = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
                        BillRecord incomeRecord = new BillRecord(date, amount, category, remark);
                        billManager.addIncomeRecord(incomeRecord);
                    } catch (ParseException e) {
                        System.out.println("日期格式错误，请重新输入！");
                    }
                    break;
                case "2":
                    System.out.println("请输入支出信息：");
                    System.out.print("日期（YYYY-MM-DD）：");
                    dateStr = scanner.next();
                    System.out.print("金额：");
                    amount = scanner.nextDouble();
                    System.out.print("类别（如餐饮、交通、购物等）：");
                    category = scanner.next();
                    System.out.print("备注：");
                    remark = scanner.next();
                    date = new Date(0);
                    try {
                        date = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
                        BillRecord expenseRecord = new BillRecord(date, amount, category, remark);
                        billManager.addExpenseRecord(expenseRecord);
                    } catch (ParseException e) {
                        System.out.println("日期格式错误，请重新输入！");
                    }
                    break;
                case "3":
                    System.out.println("所有收入记录：");
                    billManager.displayRecords(billManager.getIncomeRecords());
                    System.out.println("所有支出记录：");
                    billManager.displayRecords(billManager.getExpenseRecords());
                    break;
                case "4":
                    System.out.println("请输入查询账单信息：");
                    System.out.print("开始日期（YYYY-MM-DD）：");
                    dateStr = scanner.next();
                    System.out.print("结束日期（YYYY-MM-DD）：");
                    String endDateStr = scanner.next();
                    System.out.print("类别（输入null查询所有类别）：");
                    category = scanner.next();
                    System.out.println("查询收入记录：");
                    billManager.queryRecords(billManager.getIncomeRecords(), dateStr, endDateStr, category);
                    System.out.println("查询支出记录：");
                    billManager.queryRecords(billManager.getExpenseRecords(), dateStr, endDateStr, category);
                    break;
                case "5":
                    System.out.print("请输入月度预算：");
                    double budget = scanner.nextDouble();
                    billManager.setMonthlyBudget(budget);
                    break;
                case "6":
                    double totalIncome = billManager.getTotalIncome();
                    double totalExpense = billManager.getTotalExpense();
                    double remainingBudget = billManager.getRemainingBudget();
                    Map<String, Double> categoryAmount = billManager.getCategoryAmount();
                    System.out.println("月度统计报告：");
                    System.out.println("总收入：" + totalIncome);
                    System.out.println("总支出：" + totalExpense);
                    System.out.println("剩余预算：" + remainingBudget);
                    System.out.println("各类别金额：");
                    for (Map.Entry<String, Double> entry : categoryAmount.entrySet()) {
                        System.out.println(entry.getKey() + ": " + entry.getValue());
                    }
                    break;
                case "7":
                    running = false;
                    System.out.println("感谢使用个人账单管理系统，再见！");
                    break;
                default:
                    System.out.println("无效的选项，请重新输入！");
                    break;
                }
            System.out.println("按任意键返回主菜单...");
            scanner.nextLine();
        }
        scanner.close();
    }
}